/*
 * BulletConfiguration.cpp
 *
 *  Created on: Jun 24, 2011
 *      Author: sebastien
 */

#include "BulletConfiguration.h"

BulletConfiguration::BulletConfiguration() {
	///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
	this->collisionConfiguration = new btDefaultCollisionConfiguration();

	///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
	this->dispatcher = new	btCollisionDispatcher(collisionConfiguration);

	///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
	this->overlappingPairCache = new btDbvtBroadphase();

	///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
	this->solver = new btSequentialImpulseConstraintSolver;

	this->dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);

	this->dynamicsWorld->setGravity(btVector3(0,0,0));
}

BulletConfiguration::~BulletConfiguration() {
	//remove the rigidbodies from the dynamics world and delete them
	for (int i=dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
	{
		btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i];
		btRigidBody* body = btRigidBody::upcast(obj);
		if (body && body->getMotionState())
		{
			delete body->getMotionState();
		}
		dynamicsWorld->removeCollisionObject( obj );
		delete obj;
	}

	//delete collision shapes
	for (int j=0;j<collisionShapes.size();j++)
	{
		btCollisionShape* shape = collisionShapes[j];
		collisionShapes[j] = 0;
		delete shape;
	}
	//delete dynamics world
	delete dynamicsWorld;

	//delete solver
	delete solver;

	//delete broadphase
	delete overlappingPairCache;

	//delete dispatcher
	delete dispatcher;

	delete collisionConfiguration;

}

btDefaultCollisionConfiguration *BulletConfiguration::getCollisionConfiguration() const
{
    return collisionConfiguration;
}

btAlignedObjectArray<btCollisionShape*> BulletConfiguration::getCollisionShapes() const
{
    return collisionShapes;
}

btCollisionDispatcher *BulletConfiguration::getDispatcher() const
{
    return dispatcher;
}

btDiscreteDynamicsWorld *BulletConfiguration::getDynamicsWorld() const
{
    return dynamicsWorld;
}

btBroadphaseInterface *BulletConfiguration::getOverlappingPairCache() const
{
    return overlappingPairCache;
}

btSequentialImpulseConstraintSolver *BulletConfiguration::getSolver() const
{
    return solver;
}


